package com.xingda.packing.dmds.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Log4j2 ConnectionFactory
 */
public class ConnectionFactory {

    private DruidDataSource dataSource;

    private ConnectionFactory() {
        Properties properties = new Properties();
        properties.setProperty("url", "jdbc:mysql://192.168.3.120:3306/xingda_packing_new?serverTimezone=GMT%2B8&autoReconnect=true&socketTimeout=30000&useUnicode=true&characterEncoding=UTF-8&useSSL=false&tinyInt1isBit=false");
        properties.setProperty("username", "root");
        properties.setProperty("password", "ganweitech_008");

        try {
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            try {
                if (dataSource != null) {
                    /* Druid数据库源对象产生失败后，取消初始化 */
                    dataSource.close();
                }
            } catch (Exception e2) {
            }
        }
    }

    public static Connection getDatabaseConnection() throws SQLException {
        return Singleton.INSTANCE.dataSource.getConnection();
    }

    private interface Singleton {
        ConnectionFactory INSTANCE = new ConnectionFactory();
    }
}